package view.componentes;

import javax.swing.text.AttributeSet;  
import javax.swing.text.BadLocationException;  
import javax.swing.text.DocumentFilter;  

public class DocumentFilterFactory extends DocumentFilter {  

	public static final int SO_NUMEROS = 0;  
	public static final int SO_LETRAS = 1;  

	private String expressaoRegular;  

	public static DocumentFilterFactory criar(int tipo) {  

		switch (tipo) {  
		case SO_NUMEROS:  
			return new DocumentFilterFactory("[^0-9]");           
		case SO_LETRAS:  
			return new DocumentFilterFactory("[0-9]");              
		default:  
			return new DocumentFilterFactory("");             
		}

	}  

	public static DocumentFilterFactory criar(String expressaoRegular) {  
		return new DocumentFilterFactory(expressaoRegular);     
	}  

	private DocumentFilterFactory(String expressaoRegular) {  
		super();  
		this.expressaoRegular = expressaoRegular;  
	}  

	@Override  
	public void insertString(FilterBypass fb, int offset, String text,  
			AttributeSet attr) throws BadLocationException {  
		fb.insertString(offset, text.replaceAll(this.expressaoRegular, ""), attr);  
	}  

	@Override  
	public void replace(FilterBypass fb, int offset, int length, String text,  
			AttributeSet attrs) throws BadLocationException {  
		fb.replace(offset, length, text.replaceAll(this.expressaoRegular, ""), attrs);    
	}

}  